# 相关模块

# 展示商品列表
def product_list(products):
    print("商品列表如下：")
    for i, product in enumerate(products):
        print(f"编号：{i:<5}名称：{product['name']:<10}价格：{product['price']:<10}")

# 购物
def shopping(shopping_cart,products):
    while True:
        print("请输入商品编号：")
        try:
            choice_index = int(input())
            if not 0 <= choice_index < len(products):
                print(f"请输入 0-{len(products)-1}的整数")
                continue
        except ValueError:
            print("输入错误，请输入数字编号")
            continue

        print("请输入要购买的商品数量：")
        try:
            choice_num = int(input())
        except ValueError:
            print("输入错误，请输入整数")
            continue

        product = products[choice_index]
        print(f"您选择的商品是：{product['name']}\n 数量为{choice_num}\n 总价格是：{product['price']*choice_num}")
        shopping_cart.append({"product": product, "num": choice_num, "price": product['price'] * choice_num})
        print("是否继续购物？(y/n)")
        choice = input().strip().lower()
        if choice == 'n':
            print("输入【3】查看购物车")
            break



# 清空购物车
def clear_cart(shopping_cart):
    shopping_cart.clear()
    print("购物车已清空")

# 查看购物车
def view_cart(shopping_cart):
    print("购物车如下：")
    for i, item in enumerate(shopping_cart):
        print(f"编号：{i:<5}名称：{item['product']['name']:<10}价格：{item['price']:<10}")
    print("输入【5】结算")
# 结算
def statement(shopping_cart, money):
    print("购物车如下：")
    for i, item in enumerate(shopping_cart):
        print(f"编号：{i:<5}名称：{item['product']['name']:<10}价格：{item['price']:<10}")
    money -= sum([item['price'] for item in shopping_cart])
    print(f"总价为：{sum([item['price'] for item in shopping_cart])}")
    print(f"余额为：{money}")


# 结算折扣
def statement_discount(shopping_cart, discount, money):
    for i, product in enumerate(shopping_cart):
        if product["product"] == discount["item"]:
            product["price"] *= discount["discount"]

    statement(shopping_cart, money)
